# author: hulingchuan@hotmail.com
#
# Hangman
# the controller of this gem

require_relative './logger_singleton'
require_relative './connector'
require_relative './vac_builder'
require_relative './guesser'

class Hangman

  def self.run (url, id)

    logger  = LoggerSingleton.instance
    logger.info 'Are you ready ?'

    builder = VacBuilder.new
    builder.build

    connection = Connector.new url
    try_limit  = connection.start_game id

    while connection.has_next_word
      word       = connection.next_word
      candidates = builder.get_words_length word.length
      guesser    = Guesser.new candidates, connection, word, try_limit
      guesser.guess
    end

    connection.get_result

    r = nil
    while r != 'Y' and r != 'N'
      logger.info 'do you satisfy ? submit ? Y / N'
      r = gets.chomp
    end

    if r == 'Y'
      connection.submit
    end

    logger.info 'bye'
  end

end